package com.ruoyi.memo.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.memo.domain.MemoSchedule;
import com.ruoyi.memo.mapper.MemoScheduleMapper;
import com.ruoyi.memo.service.IMemoScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MemoScheduleServiceImpl implements IMemoScheduleService {

    @Autowired
    private MemoScheduleMapper memoScheduleMapper;

    @Override
    public MemoSchedule selectScheduleByUserIdAndDate(Long userId, Date scheduleDate) {
        // 1. 用户ID非空校验
        if (userId == null) {
            throw new ServiceException("用户ID不能为空");
        }
        // 2. 日期非空校验
        if (scheduleDate == null) {
            throw new ServiceException("日程日期不能为空");
        }
        return memoScheduleMapper.selectScheduleByUserIdAndDate(userId, scheduleDate);
    }

    @Override
    public MemoSchedule selectMemoScheduleById(Long id) {
        // 1. 日程ID非空校验
        if (id == null) {
            throw new ServiceException("日程ID不能为空");
        }
        return memoScheduleMapper.selectMemoScheduleById(id);
    }

    @Override
    public List<MemoSchedule> selectMemoScheduleList(MemoSchedule memoSchedule) {
        // 非空校验（避免空指针）
        if (memoSchedule == null) {
            memoSchedule = new MemoSchedule();
        }
        return memoScheduleMapper.selectMemoScheduleList(memoSchedule);
    }

    @Override
    public int insertMemoSchedule(MemoSchedule memoSchedule) {

        memoSchedule.setCreateTime(DateUtils.getNowDate());
        return memoScheduleMapper.insertMemoSchedule(memoSchedule);
    }

    @Override
    public int updateMemoSchedule(MemoSchedule memoSchedule) {

        // 4. 内容非空校验
        if (memoSchedule.getContent() == null || memoSchedule.getContent().trim().isEmpty()) {
            throw new ServiceException("日程内容不能为空");
        }
        // 5. 内容长度校验（0-2000字符）
        if (memoSchedule.getContent().length() > 2000) {
            throw new ServiceException("日程内容长度不能超过2000字符");
        }

        // 7. 设置更新时间
        memoSchedule.setUpdateTime(DateUtils.getNowDate());

        // 原逻辑：检查是否存在该用户和日期的日程，不存在则创建
        MemoSchedule scheduleByDate = memoScheduleMapper.selectScheduleByUserIdAndDate(
                SecurityUtils.getUserId(), memoSchedule.getScheduleDate());
        if (scheduleByDate == null) {
            memoSchedule.setUserId(SecurityUtils.getUserId());
            return memoScheduleMapper.insertMemoSchedule(memoSchedule);
        }
        return memoScheduleMapper.updateMemoSchedule(memoSchedule);
    }

    @Override
    public int deleteMemoScheduleByIds(Long[] ids) {
        // 1. 批量删除ID数组非空校验
        if (ids == null || ids.length == 0) {
            throw new ServiceException("请选择要删除的日程");
        }
        // 2. 检查权限（只能删除自己的日程）
        for (Long id : ids) {
            MemoSchedule schedule = memoScheduleMapper.selectMemoScheduleById(id);
            if (schedule == null) {
                throw new ServiceException("日程ID " + id + " 不存在");
            }
            if (!schedule.getUserId().equals(SecurityUtils.getUserId())) {
                throw new ServiceException("无权限删除ID为 " + id + " 的日程");
            }
        }
        return memoScheduleMapper.deleteMemoScheduleByIds(ids);
    }

    @Override
    public int deleteMemoScheduleById(Long id) {
        // 1. 单个删除ID非空校验
        if (id == null) {
            throw new ServiceException("日程ID不能为空");
        }
        // 2. 检查日程存在性
        MemoSchedule schedule = memoScheduleMapper.selectMemoScheduleById(id);
        if (schedule == null) {
            throw new ServiceException("日程不存在");
        }
        // 3. 检查权限
        if (!schedule.getUserId().equals(SecurityUtils.getUserId())) {
            throw new ServiceException("无权限删除此日程");
        }
        return memoScheduleMapper.deleteMemoScheduleById(id);
    }
}